package com.lmx.repoDomain.repository;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.CommonService;
import com.lmx.persistence.service.GenericService;
import com.lmx.persistence.service.WorkPlaceService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.Building;
import com.lmx.repoDomain.tmodel.Customer;
import com.lmx.repoDomain.tmodel.Floor;
import com.lmx.repoDomain.tmodel.NurseGroup;
import com.lmx.repoDomain.tmodel.Role;
import com.lmx.repoDomain.tmodel.Room;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiFindOne;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

@JsonApiResourceRepository(Building.class)
@Component
public class BuildingRepository extends BaseRepository<Building> {

	@Autowired
	private CommonService commonService;

	@Autowired
	private WorkPlaceService workPlaceService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(Building.class);
	}

	@Override
	@JsonApiSave
	public Building save(Building building) {
		if (building.getId() == null) {// create
			Building temp = workPlaceService.getBuildingByName(building.getName());
			if (temp == null) {
				dslJpaService.create(building);
				workPlaceService.createBuilding(building);
			} else {
				building.setErrcode(4);// 名字重复
				throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
			}

		} else {// update
			if (building.getDelStatus() == 1) {// 删除
				int deleteFlag = workPlaceService.checkBuildingRelations(building);
				if(deleteFlag>0){
					building.setErrcode(8);// no delete  
					throw new BusinessException(null, CommonConstants.ERROR_CODE.NO_DELETE.label(), null);
				}
				workPlaceService.deleteBuilding(building);
				// 客户端判断
			} else {
				int updateFlag = 1;
				Building oldData = workPlaceService.getBuildingById(building.getId());
				if (oldData.getName().equals(building.getName())) {

				} else {
					Building temp = workPlaceService.getBuildingByName(building.getName());
					if (temp != null) {
						updateFlag = 0;
					}
				}
				if (updateFlag == 1) {
					// update
					Building buildingTemp = workPlaceService.getBuildingById(building.getId());
					workPlaceService.updateBuilding(buildingTemp,building);
				} else {
					building.setErrcode(4);
					throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
				}
			}
		}
		return building;
	}

	@Override
	@JsonApiFindOne
	public Building findOne(final Long id, final QueryParams requestParams) {
		Building data = null;
		data = (Building) dslJpaService.findById(id);
		data = workPlaceService.countBuildingBedNum(data);
		List<Floor> floors = new ArrayList<>();
		floors= workPlaceService.initialBuiidingData(data);//  楼层 房间 床位数据
		data.setFloors(floors);
		int roomNum=0;
		for(Floor floor:floors){
			if(floor.getRooms()!=null){
				roomNum+=floor.getRooms().size();//房间数量
			}
		}
		List<Room> publicRooms = new ArrayList<>();
		publicRooms=workPlaceService.getPublicRoom(data);
		int publicRoomNum=0;
		if(publicRooms!=null){
			publicRoomNum=publicRooms.size();
		}
		data.setRoomNum(roomNum-publicRoomNum);
		return data;

	}

	@Override
	@JsonApiFindAll
	public Iterable<Building> findAll(final QueryParams requestParams) {
		Iterable<Building> dataList = new ArrayList<>();
		dataList = super.findAll(requestParams);
		for (Building data : dataList) {
			if (data != null) {
				workPlaceService.countBuildingBedNum(data);
			}
		}
		return dataList;
	}

}
